<?php
require_once('../db.php');
pr($_POST);
pr($_FILES);
$msg = '';
  $validExtn = array('png',
                     'jpeg',
                     'jpg',
                     'gif' );
  $mb = 1;
  $allowedFileSize = $mb * 1024 * 1024;
  $uploadDir = './uploads';
if(isset($_FILES['avatar']['error']) && $_FILES['avatar']['error']==0) {
  if($_FILES['avatar']['size'] < $allowedFileSize)
  {

    $fileName = $_FILES['avatar']['name'];
    //pr($fileName);
    $fileDetails = pathinfo($fileName);
    $fileExtn = $fileDetails['extension'];
    //pr($fileExtn);
    if(!in_array($fileExtn,$validExtn)) {
      $msg = "Please uplaod file with valid extn(".implode(',',$validExtn).")";
    } else {
      $src = $_FILES['avatar']['tmp_name'];
      $fileName = date('ymdhis').'_'.uniqid().'_'.$_FILES['avatar']['name'];

      if(!is_dir($uploadDir)) {
        mkdir($uploadDir,0755);
      }
      $dest = "$uploadDir/$fileName";

      $isMove = move_uploaded_file($src, $dest);
      //var_dump($isMove);
      if($isMove == true) {
        $msg = "File uploaded successfully.";
      } else {
        $msg = "There might be some error during upload.Please try again.";
      }
    }
  } else {
    $msg = "Please upload file with $mb MB Max";
  }
} else if(isset($_FILES['avatar']['error'])){
    $msg = "Please upload file with $mb MB Max";
}

if($msg != '') {
  echo "<div class=\"error\">$msg</div>";
}
?>
<form action=""
enctype="multipart/form-data"
method="post">
  <input type="file" name="avatar" />
  <input type="submit" value="Upload" name="sbmt"/>
</form>
<style type="text/css">
  .error{
    border:1px solid red;
    padding:10px;
    margin:10px;
    background-color:pink;
    border-radius:5px;
  }
</style>
